package vip.wangwenhao.mojo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import vip.wangwenhao.util.DatabaseUtil;
import vip.wangwenhao.util.GenEntityUtil;
import vip.wangwenhao.util.GenMapperUtil;
import vip.wangwenhao.util.GenMapperXmlUtil;

import java.util.List;

/**
 * @author wwh
 * @date 2019年12月04日 11:01
 */

@Mojo(name = "mybatis-general")
public class GeneralMojo extends AbstractMojo {

    @Parameter
    private String url;
    @Parameter
    private String username;
    @Parameter
    private String password;
    @Parameter
    private List<String> tablePrefixes;
    @Parameter
    private List<String> columnPrefixes;
    @Parameter
    private String beanPackage;
    @Parameter
    private String mapperPackage;
    @Parameter
    private String mapperXmlPath;
    @Parameter
    private String author;
    @Parameter
    private List<String> tables;

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        init();
        if (CollectionUtils.isEmpty(tables)) {
            tables = DatabaseUtil.getTableNames();
        }
        for (String tableName : tables) {
            GenEntityUtil.createEntity(tableName);
            GenMapperUtil.createMapper(tableName);
            GenMapperXmlUtil.createMapperXml(tableName);
        }
    }

    private void init() {
        DatabaseUtil.init(this);
        GenEntityUtil.init(this);
        GenMapperUtil.init(this);
        GenMapperXmlUtil.init(this);
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBeanPackage() {
        return beanPackage;
    }

    public void setBeanPackage(String beanPackage) {
        this.beanPackage = beanPackage;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public List<String> getTables() {
        return tables;
    }

    public void setTables(List<String> tables) {
        this.tables = tables;
    }

    public String getMapperPackage() {
        return mapperPackage;
    }

    public void setMapperPackage(String mapperPackage) {
        this.mapperPackage = mapperPackage;
    }

    public String getMapperXmlPath() {
        return mapperXmlPath;
    }

    public void setMapperXmlPath(String mapperXmlPath) {
        this.mapperXmlPath = mapperXmlPath;
    }

    public List<String> getTablePrefixes() {
        return tablePrefixes;
    }

    public void setTablePrefixes(List<String> tablePrefixes) {
        this.tablePrefixes = tablePrefixes;
    }

    public List<String> getColumnPrefixes() {
        return columnPrefixes;
    }

    public void setColumnPrefixes(List<String> columnPrefixes) {
        this.columnPrefixes = columnPrefixes;
    }
}